package com.yuyuan.blog.utils;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * id生成类(博客、用户、评论)
 */
@Component
public class IdGenerator {
    public static final long BEGIN_TIMESTAMP = 385577600;

    private static final long COMMENT_BEGIN_TIMESTAMP = 1690848000;//23.8.1

    private static final long USER_REGISTER_TIMESTAMP = 1640995200;//22.1.1

    private static final long LIKE_TIMESTAMP = 1640995200;//22.1.1

    private static final long NOTICE_TIMESTAMP = 1640995200;//22.1.1

    private static final long FOLLOW_TIMESTAMP = 946684800;//00.1.1

    /** ID位数 */
    public static final long BIT_COUNT = 0;

    /**
     * RedisID生成算法
     */
    public long nextBlogId(Short blogType){
        //生成时间戳
        LocalDateTime curTime = LocalDateTime.now();
        long timestamp = curTime.toEpochSecond(ZoneOffset.UTC) -BEGIN_TIMESTAMP * (blogType + 1);
        //生成序列号
        //拼接并返回(可以用位运算进行拼接)
        //具体思路是先左移后做或运算
        return timestamp << BIT_COUNT;
    }

    public long nextCommentId(){
        //生成时间戳
        LocalDateTime curTime = LocalDateTime.now();
        long timestamp = curTime.toEpochSecond(ZoneOffset.UTC) - COMMENT_BEGIN_TIMESTAMP;
        //生成序列号
        //拼接并返回(可以用位运算进行拼接)
        //具体思路是先左移后做或运算
        return timestamp << BIT_COUNT;
    }

    public long nextUserId(){
        //生成时间戳
        LocalDateTime curTime = LocalDateTime.now();
        long timestamp = curTime.toEpochSecond(ZoneOffset.UTC) - USER_REGISTER_TIMESTAMP;
        //生成序列号
        //拼接并返回(可以用位运算进行拼接)
        //具体思路是先左移后做或运算
        return timestamp << BIT_COUNT;
    }

    public long nextLikeId(){
        //生成时间戳
        LocalDateTime curTime = LocalDateTime.now();
        long timestamp = curTime.toEpochSecond(ZoneOffset.UTC) - LIKE_TIMESTAMP;
        //生成序列号
        //拼接并返回(可以用位运算进行拼接)
        //具体思路是先左移后做或运算
        return timestamp << BIT_COUNT;
    }

    public long nextNoticeId(){
        //生成时间戳
        LocalDateTime curTime = LocalDateTime.now();
        long timestamp = curTime.toEpochSecond(ZoneOffset.UTC) - LIKE_TIMESTAMP;
        //生成序列号
        //拼接并返回(可以用位运算进行拼接)
        //具体思路是先左移后做或运算
        return timestamp << BIT_COUNT;
    }

    public long nextFollowId(){
        //生成时间戳
        LocalDateTime curTime = LocalDateTime.now();
        long timestamp = curTime.toEpochSecond(ZoneOffset.UTC) - FOLLOW_TIMESTAMP;
        //生成序列号
        //拼接并返回(可以用位运算进行拼接)
        //具体思路是先左移后做或运算
        return timestamp << BIT_COUNT;
    }

    public static void main(String[] args) {
        LocalDateTime time = LocalDateTime.of(2000,1,1,0,0,0);
        long second = time.toEpochSecond(ZoneOffset.UTC);
        System.out.println(second);
    }
}
